package mo.leetcode_top;

public class L42 {

    public int trap(int[] height) {
        int res = 0;
        int left = 0;
        int right = height.length - 1;
        while (left < right) {
           int min = Math.min(height[left], height[right]);
           if (height[left] == min) {
               left++;
               while (left < right && height[left] < min) {
                   res += min - height[left++];
               }
           } else {
               right--;
               while (right > left && height[right] < min) {
                   res += min - height[right--];
               }
           }
        }
        return res;
    }

    public static void main(String[] args) {
        L42 test = new L42();
        System.out.println(test.trap(new int[]{4,2,0,3,2,5}));
    }

}
